package com.jp.service.impl;

import com.jp.service.GitLogPrint;
import com.jp.utils.LogUtils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class MacGitLogPrint implements GitLogPrint {

    @Override
    public void logPrint(String fileAddress, String type) throws IOException {
        Stream<String> stream = Files.lines(Paths.get(fileAddress));

        List<Map<String, String>> list = stream.filter(item -> item.contains("commit:")).map(LogUtils::getMsgInfo).filter(Objects::nonNull).collect(Collectors.toList());

        // 根据日期分组
        Map<String, List<String>> infoByGroup = LogUtils.getInfoByGroup(list);

        // 获得所有要输出的日期
        List<String> dates = LogUtils.getDates(type);

        // 控制如果没有合适的数据输出则输出 "暂无数据"
        int a = 0;

        for (Map.Entry<String, List<String>> entry : infoByGroup.entrySet()) {
            if (dates.size() > 0) {
                // 需要根据指定日期输出
                if (dates.contains(entry.getKey())) {
                    System.out.println(entry.getKey());
                    entry.getValue().forEach(System.out::println);
                    System.out.println();
                    a++;
                }
            } else {
                // 不需要根据指定日期输出
                System.out.println(entry.getKey());
                entry.getValue().forEach(System.out::println);
                if (entry.getValue().size() > 0) {
                    a++;
                }
                System.out.println();
            }
        }
    }
}
